﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.SampleWeb.Models
{
    public static partial class ExtensionMethods
    {
        /// <summary>
        /// Prefixes for numbers
        /// Since a long can only hold 9.2E18 (ie., exa territory) we
        /// don't need zetta, yotta, xona, weka, vunda, uda, treda, sorta, 
        /// rinta, quexa, pepta, ocha, nena, minga, or luma
        /// Just so you know...
        /// </summary>
        private static string[] sizes = { " ", "K", "M", 
            "G", "T", "P", "E" };
        /// <summary>
        /// Format a set of bytes into a human readable format
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToReadableSIUnit(this long value)
        {
            // Get the exponent
            // Since logn(x) = y means "Multiply 'n' by itself 'y' times to get 'x'",
            // the integer part of the log base 10 of any number is the exponent.
            // (This is called the "characteristic" in math parlance)
            // Since we are using longs which don't have a faractional part, this can't be negative.
            int exponent = (int)Math.Log10(value);
            int group = exponent / 3;
            if (group >= sizes.Length)
            {
                throw new ApplicationException("64 bit numbers are bigger than they should be...");
            }
            double divisor = Math.Pow(10, group * 3);
            return string.Format("{0:0.0} {1}", value / divisor, sizes[group]);
        }
    }

}
